package com.yao.club.service.impl;

import com.yao.club.entity.dto.*;
import com.yao.club.entity.result.ResponseEntity;
import com.yao.club.entity.vo.GoodsAttrValueVO;
import com.yao.club.entity.vo.GoodsDetailVO;
import com.yao.club.entity.vo.InformationDetailVO;
import com.yao.club.entity.vo.SpecAttrVO;
import com.yao.club.reposittory.*;
import com.yao.club.service.GoodsDetailService;
import com.yao.club.utils.ResponseUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import org.springframework.data.domain.Pageable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsDetailServiceImpl implements GoodsDetailService {
    @Autowired
    private GoodsMainRepository goodsMainRepository;
    @Autowired
    private GoodsSkuRepository goodsSkuRepository;
    @Autowired
    private GoodsAttrRepository goodsAttrRepository;
    @Autowired
    private GoodsAttrValueRepository goodsAttrValueRepository;
    @Autowired
    private MallOrderNearlyRepository mallOrderNearlyRepository;
    @Autowired
    private MallGoodsInfoRepository mallGoodsInfoRepository;


    @Override
    public ResponseEntity<GoodsDetailVO> getGoodsDetail(Long goodsId) {
        try {
            // 获取商品主信息
            GoodsMain goodsMain = goodsMainRepository.findById(goodsId)
                    .orElseThrow(() -> new RuntimeException("商品不存在"));

            GoodsDetailVO detailVO = new GoodsDetailVO();
            // 设置基本信息
            BeanUtils.copyProperties(goodsMain, detailVO);

            // 获取商品SKU列表
            List<GoodsSku> skuList = goodsSkuRepository.findGoodsSkusByGoodsId(goodsId);
            detailVO.setSkuList(skuList);

            // 获取规格属性列表
            List<SpecAttrVO> specAttrList = new ArrayList<>();
            List<GoodsAttr> attrs = goodsAttrRepository.findGoodsAttrByGoodsId(goodsId);

            for (GoodsAttr attr : attrs) {
                SpecAttrVO specAttr = new SpecAttrVO();
                specAttr.setId(attr.getId().toString());
                specAttr.setAttrKey(attr.getAttrKey());
                specAttr.setIsShowPic(attr.getIsShowPic());

                // 获取属性值列表
                List<GoodsAttrValue> attrValues = goodsAttrValueRepository.findGoodsAttrValueByAttrId(attr.getId());

                List<GoodsAttrValueVO> attrValueVOs = attrValues.stream()
                        .map(value -> {
                            GoodsAttrValueVO valueVO = new GoodsAttrValueVO();
                            valueVO.setId(value.getId().toString());
                            valueVO.setAttrKeyId(value.getAttrId()+"");
                            valueVO.setAttrValue(value.getAttrValue());
                            valueVO.setFileUrl(value.getFileUrl());
                            return valueVO;
                        })
                        .collect(Collectors.toList());

                specAttr.setGoodsAttrValues(attrValueVOs);
                specAttrList.add(specAttr);
            }
            detailVO.setSpecAttrList(specAttrList);

            // 处理图片和视频URL列表
            if (goodsMain.getDetailUrl() != null) {
                detailVO.setDetailUrl(Arrays.asList(goodsMain.getDetailUrl().split(",")));
            }
            if (goodsMain.getPosterUrl() != null) {
                detailVO.setPosterUll(Arrays.asList(goodsMain.getPosterUrl().split(",")));
            }
            if (goodsMain.getVideoUrl() != null) {
                detailVO.setVideoUrl(Arrays.asList(goodsMain.getVideoUrl().split(",")));
            }

            return ResponseUtils.success(detailVO);
        } catch (Exception e) {
            return ResponseUtils.failed("获取商品详情失败：" + e.getMessage());
        }
    }

    @Override
    public ResponseEntity<List<MallGoodsInfo>> mallGoodsListRecommend() {
        // 创建分页请求
        Pageable pageable = PageRequest.of(0, 10);
        Page<MallGoodsInfo> all = mallGoodsInfoRepository.findAll(pageable);
        if (all != null && all.getContent() != null) {
            return ResponseUtils.success(all.getContent());
        }
        return ResponseUtils.failed("失败");
    }

    @Override
    public ResponseEntity<List<MallOrderNearly>> orderGoodsSubmitSuccess() {
        List<MallOrderNearly> all = mallOrderNearlyRepository.findAll();
        if (all != null) {
            return ResponseUtils.success(all);
        }
        return ResponseUtils.failed("失败");
    }
}
